﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Moving03.Administrator
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated == false || User.Identity.IsAuthenticated == true && !User.IsInRole("Admin"))
            {
                Response.Redirect("../Default.aspx");
            }
        }
        public static Guid lay_IDUser()
        {
            Guid userID = (Guid)Membership.GetUser().ProviderUserKey;
            return userID;
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "sua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                DropDownList loainguoidung = row.FindControl("a") as DropDownList;
                string nguoidung = loainguoidung.SelectedItem.Text;
                Label lb = row.FindControl("b") as Label;
                string namend = lb.Text;
                if (nguoidung == "Admin")
                    return;
                Guid iduser = lay_IDUser();
                var query = (from nd in context.aspnet_Users
                            where nd.UserId == lay_IDUser()
                            select nd).Single();

                string ten = query.UserName;

                if (namend == ten)
                {
                    string a = "<script language='javascript'>  window.alert('Bạn đang đăng nhập với user_name này nên không được sửa...') </script>";
                    Response.Write(a);
                    return;
                }

                if (Roles.IsUserInRole(namend, "Admin"))
                {
                    Roles.RemoveUserFromRole(namend, "Admin");
                    if (Roles.IsUserInRole(namend, "Member"))
                        Roles.RemoveUserFromRole(namend, "Member");
                    Roles.AddUserToRole(namend, "Member");
                }
                else
                {
                    Roles.AddUserToRole(namend, "Member");
                }
            }
            GridView1.DataBind();
            GridView2.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (GridView1.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList loainguoidung = (DropDownList)e.Row.Cells[0].FindControl("a");
                string s = loainguoidung.SelectedItem.Text;
                Label label = e.Row.FindControl("b") as Label;
               
            }
        }

        protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "sua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView2.Rows[index];
                DropDownList loainguoidung = row.FindControl("a") as DropDownList;
                string nguoidung = loainguoidung.SelectedItem.Text;
                Label lb = row.FindControl("b") as Label;
                string namend = lb.Text;
                if (nguoidung == "Member")
                {
                    return;
                }
                if (Roles.IsUserInRole(namend, "Member"))
                {
                    Roles.RemoveUserFromRole(namend, "Member");
                    if (Roles.IsUserInRole(namend, "Admin"))
                        Roles.RemoveUserFromRole(namend, "Admin");
                    Roles.AddUserToRole(namend, "Admin");
                }
                else
                {
                    Roles.AddUserToRole(namend, "Admin");
                }
            }

            if (e.CommandName == "xoa")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView2.Rows[index];
                Label lb = row.FindControl("b") as Label;
                string namend = lb.Text;
                var user = (from us in context.aspnet_Users
                           where us.UserName == namend
                           select us).Single();
                var role = (from r in context.aspnet_UsersInRoles
                           where r.UserId == user.UserId
                           select r).Single();

                var membership = (from m in context.aspnet_Memberships
                                  where m.UserId == user.UserId
                                  select m).Single();

                                 
                try
                {
                    context.aspnet_Memberships.DeleteOnSubmit(membership);
                    context.SubmitChanges();
                    context.aspnet_UsersInRoles.DeleteOnSubmit(role);
                    context.SubmitChanges();
                    context.aspnet_Users.DeleteOnSubmit(user);
                    context.SubmitChanges();
                }
                catch(Exception ex) {

                    string s = ex.Message;
                    string a = "<script language='javascript'>  window.alert('lỗi khóa ngoại rồi...') </script>";
                        Response.Write(a);
                }
            }
            GridView2.DataBind();
            GridView1.DataBind();
        }

       
     
    }
}